/**
 * Description: 隐患统计记录
 *
 * @author:lwx
 * @since:1.0.0 Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-03-18   lwx       1.0.0     Initial
 */
package com.hopes.bizz.risk.hid.controller;


import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.risk.hid.dto.RiskHidHiddenDangerStatisticsDto;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerStatisticsService;
import com.hopes.bizz.risk.hid.vo.RiskHidHiddenDangerStatisticsColumnVo;
import com.hopes.bizz.risk.hid.vo.RiskHidHiddenDangerStatisticsVo;
import com.hopes.boss.common.core.util.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * 隐患统计记录<br>
 *
 * @version [1.0.0, 2022-03-18]
 * @author:lwx
 * @since:1.0.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hidStatistics")
@Tag(description = "hidStatistics", name = "隐患统计管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidHiddenDangerStatisticsController {


    private final RiskHidHiddenDangerStatisticsService hidHiddenDangerStatisticsService;


    /**
     * 默认查询条件设置，没有结束时间，默认当前时间.没有开始时间，默认当前时间前推12个月
     *
     * @param queryParams
     */
    private void setQueryParam(RiskHidHiddenDangerStatisticsDto queryParams) {
        // 默认查询条件设置，没有结束时间，默认当前时间
        if (Objects.isNull(queryParams.getEndTime())) {
            queryParams.setEndTime(new Date());
        } else {
            // 查询条件没有时分秒，增加一天，以便选出当天的数据
            queryParams.setEndTime(new Date(queryParams.getEndTime().getTime() + 24 * 60 * 60 * 1000));
        }
        // 默认查询条件设置，没有开始时间，默认当前时间前推12个月
        if (Objects.isNull(queryParams.getBeginTime())) {
            Date d = new Date(DateUtils.getLastMonth(queryParams.getEndTime(), -12, new SimpleDateFormat("yyyy/MM/dd")));
            queryParams.setBeginTime(d);
        }
    }

    /**
     * 查询隐患部门统计记录
     *
     * @author lwx
     * @version [1.0.0, 2022-05-26]
     * @since [1.0.0]
     */
    @ApiOperation("查询隐患部门统计记录列表")
    @PostMapping(value = "/deptInfo")
    @PreAuthorize("@pms.hasPermission('hid_statistics_view')" )
    public R deptInfo(@RequestBody RiskHidHiddenDangerStatisticsDto queryParams) {
        setQueryParam(queryParams);
        RiskHidHiddenDangerStatisticsVo hidHiddenDangerStatisticsVo = hidHiddenDangerStatisticsService.deptInfo(queryParams);
        return R.ok(hidHiddenDangerStatisticsVo);
    }

    /**
     * 查询隐患变化趋势统计记录
     *
     * @author lwx
     * @version [1.0.0, 2022-05-26]
     * @since [1.0.0]
     */
    @ApiOperation("查询隐患变化趋势统计记录列表")
    @PostMapping(value = "/changeTrendInfo")
    @PreAuthorize("@pms.hasPermission('hid_statistics_view')" )
    public R changeTrendInfo(@RequestBody RiskHidHiddenDangerStatisticsDto queryParams) {
        setQueryParam(queryParams);
        RiskHidHiddenDangerStatisticsVo hidHiddenDangerStatisticsVo = hidHiddenDangerStatisticsService.changeTrendInfo(queryParams);
        return R.ok(hidHiddenDangerStatisticsVo);
    }

    /**
     * 查询隐患类别统计记录
     *
     * @author lwx
     * @version [1.0.0, 2022-05-26]
     * @since [1.0.0]
     */
    @ApiOperation("查询隐患类别统计记录列表")
    @PostMapping(value = "/hidTypeInfo")
    @PreAuthorize("@pms.hasPermission('hid_statistics_view')" )
    public R hidTypeInfo(@RequestBody RiskHidHiddenDangerStatisticsDto queryParams) {
        setQueryParam(queryParams);
        RiskHidHiddenDangerStatisticsVo hidStatisticsPicList = hidHiddenDangerStatisticsService.hidTypeInfo(queryParams);
        return R.ok(hidStatisticsPicList);
    }

    /**
     * 查询隐患部门统计记录
     *
     * @author lwx
     * @version [1.0.0, 2022-05-26]
     * @since [1.0.0]
     */
    @ApiOperation("查询风险点类型统计记录列表")
    @PostMapping(value = "/riskPointInfo")
    @PreAuthorize("@pms.hasPermission('hid_statistics_view')" )
    public R riskPointInfo(@RequestBody RiskHidHiddenDangerStatisticsDto queryParams) {
        setQueryParam(queryParams);
        RiskHidHiddenDangerStatisticsVo hidStatisticsTableList = hidHiddenDangerStatisticsService.riskPointInfoForTable(queryParams);
        return R.ok(hidStatisticsTableList);
    }


    @ApiOperation("隐患整改动态管控-本年度")
    @GetMapping(value = "/hidTypeInfoYear")
    @PreAuthorize("@pms.hasPermission('hid_statistics_view')" )
    public R hidTypeInfoYear() {
        final RiskHidHiddenDangerStatisticsColumnVo hidHiddenDangerStatisticsColumnVo = hidHiddenDangerStatisticsService.hidTypeInfoYear();
        return R.ok(hidHiddenDangerStatisticsColumnVo);
    }
}
